home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 002 / make / file.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  9KB  |  375 lines

  1. #include <stdio.h>
  2. #include "make.h"
  3.  
  4. static FILENODE *addtolist ();
  5. static FILENODE *afnode ();
  6.  
  7. /*
  8.  * Return file-node for 'fname'.
  9.  * If it doesn't exist, then create one.
  10.  */
  11.  
  12. FILENODE *filenode (fname)
  13. char *fname;
  14. {
  15.     register FILENODE *f;
  16.  
  17.     DBUG_ENTER ("filenode");
  18.     DBUG_3 ("fnode", "locate or create node for file '%s'", fname);
  19.     if ((f = gfile (fname)) == (FILENODE *) NULL) {
  20.     f = afnode (fname);
  21.     }
  22.     DBUG_RETURN (f);
  23. }
  24.  
  25. /*
  26.  * Add a dependency to the node 'fnd'.
  27.  * 'fnd' will depend on 'fname'.
  28.  *
  29.  * Returns dependent node associated with 'fname'.
  30.  */
  31.  
  32. FILENODE *addfile (fnd, fname)
  33. FILENODE *fnd;
  34. char *fname;
  35. {
  36.     register NODE *n;
  37.     register FILENODE *f;
  38.  
  39.     DBUG_ENTER ("addfile");
  40.     if (fnd == (FILENODE *) NULL) {    /* punt if no root file */
  41.     fputs ("No current root, can't add dependency '", stderr);
  42.     fputs (fname, stderr);
  43.     fputs ("%s'\n", stderr);
  44.     DBUG_RETURN ((FILENODE *) NULL);
  45.     }
  46.     DBUG_4 ("fdep", "add dependency of '%s' on '%s'", fnd -> fname, fname);
  47.     f = filenode (fname);
  48.     if ((n = (NODE *) Calloc (1, sizeof (NODE))) == (NODE *) NULL) {
  49.     allerr ();
  50.     }
  51.     n -> nnext = fnd -> fnode;
  52.     fnd -> fnode = n;
  53.     n -> nfile = f;
  54.     DBUG_RETURN (f);
  55. }
  56.  
  57.  
  58. /*
  59.  * Add a line of method-text to the node 'fnode'.
  60.  */
  61.  
  62. void addmeth (fnode, methtext)
  63. FILENODE *fnode;
  64. char *methtext;
  65. {
  66.     register int len;
  67.     register char *new;
  68.     extern void free ();
  69.  
  70.     DBUG_ENTER ("addmeth");
  71.     if (fnode != (FILENODE *) NULL && methtext != NULL) {
  72.     len = strlen (methtext) + 2;
  73.     if (fnode -> fmake == NULL) {
  74.         if ((fnode -> fmake = (char *) Calloc (1, 1)) == NULL) {
  75.         allerr ();
  76.         }
  77.         *(fnode -> fmake) = '\0';
  78.     }
  79.     len += strlen (fnode -> fmake);
  80.     /* Lattice C does not have 'realloc()', so this kludges around it: */
  81.     if ((new = (char *) Calloc (1, len)) == NULL) {
  82.         allerr ();
  83.     }
  84.     strcpy (new, fnode -> fmake);
  85.     free (fnode -> fmake);
  86.     fnode -> fmake = new;
  87.     strcat (fnode -> fmake, methtext);
  88.     len = strlen (fnode -> fmake);
  89.     if (len > 0 && fnode -> fmake[len - 1] != '\n') {
  90.         strcat (fnode -> fmake, "\n");
  91.     }
  92.     }
  93.     DBUG_VOID_RETURN;
  94. }
  95.  
  96. /*
  97.  * Add token to the parent list.  Return the pointer to the new parent.
  98.  * If token is already on the parent list, simply return the pointer found.
  99.  */
  100.  
  101. static FILENODE *addtolist (tok, list)
  102. char *tok;
  103. NODE **list;
  104. {
  105.     register NODE *search;
  106.     register NODE *newnode;
  107.  
  108.     DBUG_ENTER ("addtolist");
  109.     for (search = *list; search != (NODE *) NULL; search = search -> nnext) {
  110.     if (STRSAME (search -> nfile -> fname, tok)) {
  111.         DBUG_RETURN (search -> nfile);
  112.     }
  113.     }
  114.     /* token not found so far... add it to list */
  115.     if ((newnode = (NODE *) Calloc (1, sizeof (NODE))) == (NODE *) NULL) {
  116.     allerr ();
  117.     }
  118.     search = *list;
  119.     *list = newnode;
  120.     newnode -> nnext = search;
  121.     if ((newnode -> nfile = (FILENODE *) Calloc (1, sizeof (FILENODE))) == (FILENODE *) NULL) {
  122.     allerr ();
  123.     }
  124.     if ((newnode -> nfile -> fname = (char *) Calloc (1, strlen (tok) + 1)) == NULL) {
  125.     allerr ();
  126.     }
  127.     strcpy (newnode -> nfile -> fname, tok);
  128.     newnode -> nfile -> fdate = (DATE) NULL;
  129.     newnode -> nfile -> fnode = (NODE *) NULL;
  130.     newnode -> nfile -> parent = (FILENODE *) NULL;
  131.     newnode -> nfile -> fflag = 0;
  132.     newnode -> nfile -> fnext = NULL;
  133.     DBUG_RETURN (newnode -> nfile);
  134. }
  135.  
  136. static  NODE *parentlist = (NODE *) NULL;
  137.  
  138. FILENODE *addparent (tok)
  139. char *tok;
  140. {
  141.     FILENODE *np;
  142.     
  143.     DBUG_ENTER ("addparent");
  144.     np = addtolist (tok, &parentlist);
  145.     DBUG_RETURN (np);
  146. }
  147.  
  148. #ifdef FUNNYLIBS
  149.  
  150. isonlibrary (f)            /* return SUCCEED if f is a library. */
  151. FILENODE *f;            /* set f->fdate to parent's date */
  152. {
  153.     DBUG_ENTER ("isonlibrary");
  154.     if (f -> fflag & LIBRARY) {
  155.     getdate (f -> parent);
  156.     f -> fdate = f -> parent -> fdate;
  157.     DBUG_RETURN (SUCCEED);
  158.     }
  159.     DBUG_RETURN (FAILURE);
  160. }
  161.  
  162. #else
  163.  
  164. /*
  165.  * Add file node fnd to library list.
  166.  */
  167.  
  168. static FILENODE *librarylist = (FILENODE *) NULL;
  169.  
  170. void AddToLibrary (fnd)
  171. FILENODE *fnd;
  172. {
  173.     register NODE *n;
  174.  
  175.     DBUG_ENTER ("AddToLibrary");
  176.     DBUG_3 ("lib", "add node for '%s' to library list", fnd -> fname);
  177.     if (librarylist == (FILENODE *) NULL) {
  178.     if ((librarylist = (FILENODE *) Calloc (1, sizeof (FILENODE))) == (FILENODE *) NULL) {
  179.         allerr ();
  180.     }
  181.     librarylist -> fnode = (NODE *) NULL;
  182.     }
  183.     if ((n = (NODE *) Calloc (1, sizeof (NODE))) == (NODE *) NULL) {
  184.     allerr ();
  185.     }
  186.     n -> nnext = librarylist -> fnode;
  187.     librarylist -> fnode = n;
  188.     n -> nfile = fnd;
  189.     DBUG_VOID_RETURN;
  190. }
  191.  
  192. /*
  193.  * Return SUCCEED if filenode f is a direct descendant of a library;
  194.  * set f->fdate to parent's time.
  195.  */
  196.  
  197. isonlibrary (f)
  198. FILENODE *f;
  199. {
  200.     register NODE *lib;
  201.     register NODE *dep;
  202.  
  203.     DBUG_ENTER ("isonlibrary");
  204.     DBUG_3 ("isonlib", "Searching for: %s", f -> fname);
  205.     if (librarylist == (FILENODE *) NULL) {
  206.     DBUG_RETURN (FAILURE);
  207.     }
  208.     for (lib = librarylist->fnode; lib != (NODE *)NULL; lib = lib->nnext) {
  209.     for (dep = lib->nfile->fnode; dep != (NODE *)NULL; dep = dep->nnext) {
  210.         DBUG_3 ("dep", "Examining: %s", dep -> nfile -> fname);
  211.         if (f == dep -> nfile) {        /* found it!! */
  212.         DBUG_3 ("dep", "found %s", dep -> nfile -> fname);
  213.         DBUG_3 ("dep", "depends on %s", lib -> nfile -> fname);
  214.         f -> fdate = lib -> nfile -> fdate;    /* update time */
  215.         DBUG_RETURN (SUCCEED);
  216.         }
  217.     }
  218.     }
  219.     DBUG_RETURN (FAILURE);
  220. }
  221. #endif
  222.  
  223. isanarchive (f)            /* return SUCCEED if f is an archive */
  224. FILENODE *f;            /* set f->fdate to date in parent's */
  225. {                /* archive directory */
  226.     DATE getarchdate ();
  227.  
  228.     DBUG_ENTER ("isanarchive");
  229.     if (f -> fflag & ARCHIVE) {
  230.     f -> fdate = getarchdate (f -> parent -> fname, f -> fname);
  231.     DBUG_RETURN (SUCCEED);
  232.     }
  233.     DBUG_RETURN (FAILURE);
  234. }
  235.  
  236. NODE *deletelist = (NODE *) NULL;
  237.  
  238. #ifdef LAR
  239. extract (f)
  240. FILENODE *f;
  241. {
  242.     DBUG_ENTER ("extract");
  243.     DBUG_3 ("extr", "extracting %s for archivehood", f -> fname);
  244.     if (f -> fflag & ARCHIVE) {
  245.     DBUG_3 ("extr", "copying %s for archivehood", f -> fname);
  246. #ifndef NOREALEXTRACT
  247.     copyfile (f -> parent -> fname, f -> fname);
  248. #endif
  249.     /* delete f->fname at end of run */
  250.     (void) addtolist (f -> fname, &deletelist);
  251.     DBUG_RETURN (SUCCEED);
  252.     }
  253.     DBUG_RETURN (FAILURE);
  254. }
  255. #endif
  256.  
  257. void cleanuparchives ()
  258. {
  259.     register NODE *search;
  260.  
  261.     DBUG_ENTER ("cleanuparchives");
  262.     for (search=deletelist; search != (NODE *)NULL; search = search->nnext) {
  263.     fputs ("Purging ", stdout);
  264.     puts (search -> nfile -> fname);
  265. #ifndef NOREALDELETE
  266.     unlink (search -> nfile -> fname);
  267. #endif
  268.     }
  269.     DBUG_VOID_RETURN;
  270. }
  271.  
  272.  
  273. /*
  274.  * Get a filenode for the file called 'fn'.
  275.  * Returns (FILENODE *) NULL if the node doesn't exist.
  276.  */
  277.  
  278. FILENODE *gfile (fn)
  279. char *fn;
  280. {
  281.     register FILENODE *f;
  282.  
  283.     DBUG_ENTER ("gfile");
  284.     DBUG_3 ("fnode", "look for file node '%s'", fn);
  285.     for (f = froot; f != (FILENODE *) NULL; f = f -> fnext) {
  286.     if (STRSAME (fn, f -> fname)) {
  287.         DBUG_2 ("fnode", "found it");
  288.         break;
  289.     }
  290.     }
  291.     DBUG_RETURN (f);
  292. }
  293.  
  294.  
  295. /*
  296.  * Alloc space for a new file node.
  297.  *
  298.  * Note that when this routine is called, it has already been
  299.  * determined that there is no existing node with this name, so
  300.  * we don't need to check again.
  301.  *
  302.  * Also note that the method string pointer is set to a dynamically
  303.  * allocated null string, rather than a static null string (""), because
  304.  * it is latter freed when expanding the method string.
  305.  *
  306.  */
  307.  
  308. static FILENODE *afnode (name)
  309. char *name;
  310. {
  311.     FILENODE *f;
  312.  
  313.     DBUG_ENTER ("afnode");
  314.     DBUG_3 ("fnode", "allocate a new node for file '%s'", name);
  315.     if ((f = (FILENODE *) Calloc (1, sizeof (FILENODE))) == (FILENODE *) NULL) {
  316.     allerr ();
  317.     }
  318.     if ((f -> fname = (char *) Calloc (1, strlen (name) + 1)) == NULL) {
  319.     allerr ();
  320.     }
  321.     strcpy (f -> fname, name);
  322.     if ((f -> fmake = (char *) Calloc (1, 1)) == NULL) {
  323.     allerr ();
  324.     }
  325.     *(f -> fmake) = '\0';
  326.     f -> fdate = (DATE) NULL;
  327.     f -> fnode = (NODE *) NULL;
  328.     f -> parent = (FILENODE *) NULL;
  329.     f -> fflag = 0;
  330.     f -> fnext = froot;
  331.     froot = f;
  332.     DBUG_RETURN (f);
  333. }
  334.  
  335. /*
  336.  * Print dependency tree.
  337.  */
  338.  
  339. void prtree ()
  340. {
  341.     register FILENODE *f;
  342.     register NODE *n;
  343.     extern char *printdate ();
  344.  
  345.     DBUG_ENTER ("prtree");
  346.     for (f = froot; f != (FILENODE *) NULL; f = f -> fnext) {
  347.     fputs (f -> fname, stdout);
  348.     fputs ((f -> fflag & ROOTP) ? " (root)" : "", stdout);
  349.     fputs ((f -> fflag & REBUILT) ? " (rebuilt)" : "", stdout);
  350.     fputs ((f -> fflag & LIBRARY) ? " (library)" : "", stdout);
  351.     fputs ((f -> fflag & EXTRACT) ? " (extracted)" : "", stdout);
  352.     fputs ((f -> fflag & ARCHIVE) ? " (archive)" : "", stdout);
  353.     fputs (printdate (f -> fdate), stdout);
  354.     fputc ('\n', stdout);
  355.     if (f -> parent != (FILENODE *) NULL) {
  356.         fputs ("Parent is: ", stdout);
  357.         fputs (f -> parent -> fname, stdout);
  358.         fputc ('\n', stdout);
  359.         fputs ("Parental Date:", stdout);
  360.         fputs (printdate (f -> parent -> fdate), stdout);
  361.         fputc ('\n', stdout);
  362.     }
  363.     if (f -> fmake != NULL) {
  364.         puts (f -> fmake);
  365.     }
  366.     puts ("Dependents: ");
  367.     for (n = f -> fnode; n != (NODE *) NULL; n = n -> nnext) {
  368.         fputc ('\t', stdout);
  369.         puts ((n -> nfile) -> fname);
  370.     }
  371.     fputc ('\n', stdout);
  372.     }
  373.     DBUG_VOID_RETURN;
  374. }
  375.